package com.itic.web.vedio.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.plugins.mybatis.Page;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.demo.modelfiles.persistence.model.ModelFiles;
import com.itic.demo.modelfiles.service.ModelFilesService;
import com.itic.ht.mem.member.persistence.model.WebMember;
import com.itic.ht.mem.member.service.WebMemberService;
import com.itic.ht.mem.memberdetail.persistence.model.WebMemberDetail;
import com.itic.ht.mem.memberdetail.service.WebMemberDetailService;
import com.itic.ht.mem.vedio.persistence.model.WebVedio;
import com.itic.ht.mem.vedio.persistence.model.WebVedioExample;
import com.itic.ht.mem.vedio.service.WebVedioService;
import com.itic.web.qo.TokenData;
import com.itic.web.ro.PageInfo;
import com.itic.web.ro.ResResult;
import com.itic.web.util.TokenUtil;

@Service
@Transactional
public class AppVedioService {
	

	@Autowired
	private WebMemberService webMemberService;
	@Autowired
	private WebMemberDetailService webMemberDetailService;
    @Autowired
    private ModelFilesService modelFilesService;
    @Autowired
    private WebVedioService vedioService;
	
	public ResResult getTopicList(TokenData data,String topicType,Page page){
		List<WebVedio> list = new ArrayList<WebVedio>();
		WebMember member = null;
		WebMemberDetail memberDetail = null;
		try {
			//判断token是不是存在
			if(StringHelper.isEmpty(data.getToken())){
				return ResResult.error(1, "token不能为空");
			}
			//先判断token正确性
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return ResResult.error(2, "token失效");
			}
			//page对象不能为空
	        if (page.getPageNow()>0 && page.getPageSize()>0) {
	        }else{
	        	return ResResult.error(3, "page数据不能为空");
	        }
			
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			System.out.println("现在要查询的帐号是:"+data.getAccount());
			member = webMemberService.getMemberByAccount(data.getAccount());
			memberDetail = webMemberDetailService.getMemberDetailByAccount(data.getAccount());
			WebVedioExample example = null;
			if("003".equals(topicType)){
				//获取普通视屏
				page.setRecordNow((page.getPageNow()-1)*page.getPageSize());
				example = new WebVedioExample();
				example.setOrderByClause("REORDER ASC");
				example.createCriteria().andIsFeeEqualTo(topicType).andIsEffectiveEqualTo(DBConst.TRUE);
			}
            //判断用户是不是会员 ==== 有没有看视频的权利
			if("002".equals(topicType)){
			      if(!DBConst.MEMBER_GENERAL.equals(member.getCategory()) && memberDetail.getExpireTime().before(new Date())){
			      	//是会员而没有过期时
			      }else{
			      	//不是会员的 还有会员卡过期有都会提示无法=========您暂时没有权限，请购买会员卡或会员卡已过期
			      	ResResult.error(3, "您暂时没有权限，请购买会员卡或会员卡已过期 .");
			      }
			}
			if("001".equals(topicType)||"002".equals(topicType)){
				page.setRecordNow((page.getPageNow()-1)*page.getPageSize());
				example = new WebVedioExample();
				example.setOrderByClause("is_fee asc , REORDER ASC");
				example.createCriteria().andIsFeeNotEqualTo("003").andIsEffectiveEqualTo(DBConst.TRUE);
				// 获取信息总条数
			}
			example.setPage(page);
			int total = vedioService.count(example);
			list = vedioService.list(example);
			if(list!=null && list.size()>0){
                 /*for (WebVedio webVedio : list) {
                 	String fileId = webVedio.getFileId();
                 	if(fileId!=null && !"".equals(fileId)){
                 		ModelFiles file = modelFilesService.getFile(fileId);
                 		     if(file!=null){
                 		         webVedio.setFileId(file.getFilePath());
                 		         webVedio.setVedioUrl(file.getVedioPath());
                 		         Integer longTime = Integer.parseInt(file.getFileTime());
                 		         Integer minute = longTime/60;
                 		         Integer second = longTime%60;
                 		         StringBuffer sf= new StringBuffer();
                 		         sf.append(minute + ":" + second);
                 		         webVedio.setFileTime(sf.toString());
                 		         webVedio.setStyleText(file.getFileSuffix());
                 		     }
                 	}
			     }*/
			}else{
				list = new ArrayList<WebVedio>();
			}
            
            PageInfo instance = PageInfo.getInstance(page.getPageNow(), page.getPageSize(), total);
            
			return ResResult.build(200, "查询成功",list,instance);
		} catch (Exception e) {
			//返回错误代码
			e.printStackTrace();
			return ResResult.error(4, "查询失败");
		}

	}
	public ResResult getDetail(TokenData data,String topicType,String id){
		
		WebMember member = null;
		try {
			if(StringHelper.isEmpty(data.getToken())){
				return ResResult.error(1, "token不能为空");
			}
			//先判断
			boolean flag = TokenUtil.assertUserToken(data);
			if(!flag){
				return ResResult.error(2, "token失效");
			}
			//从token中取account信息   
			data.setAccount(TokenUtil.getUserList(data.getToken()).getAccount().trim());
			//取得用户信息
			System.out.println("现在要查询的帐号是:"+data.getAccount());
			member = webMemberService.getMemberByAccount(data.getAccount());
			if("002".equals(topicType)){
				if(!"eba618aea92c49dba49cc82d70be52b0".equals(member.getCategory()) && member.getExpireTime().after(new Date())){
					//是会员而没有过期时
				}else{
					//不是会员的 还有会员卡过期有都会提示无法=========您暂时没有权限，请购买会员卡或会员卡已过期
					ResResult.error(4, "您暂时没有权限，请购买会员卡或会员卡已过期 .");
				}
			}
			System.out.println(member);
			
			//查询实体数据
			WebVedio vedio = vedioService.get(id);
			//vedio.setFileId(modelFilesService.getFile(vedio.getFileId()).getFilePath());
			//vedio.setVedioUrl(modelFilesService.getFile(vedio.getFileId()).getVedioPath());
			
			return ResResult.build(200, "查询成功",vedio);
		} catch (Exception e) {
			//返回错误代码
			e.printStackTrace();
			return ResResult.error(4, "查询失败");
		}
		
	}

}
